package com.yuyou.fn.crm.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Principals;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.crm.entity.WorkSetupTemplate;
import com.yuyou.fn.crm.mapper.WorkSetupTemplateMapper;
import com.yuyou.fn.crm.service.IWorkSetupTemplateService;
import com.yuyou.fn.crm.vo.WorkSetupTemplateSaveOrUpdateVo;
import com.yuyou.fn.platform.permission.DataPermission;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.regex.Pattern;

/**
 *
 * WorkSetupTemplate 表数据服务层接口实现类
 *
 */
@Service
public class WorkSetupTemplateServiceImpl extends SuperServiceImpl<WorkSetupTemplateMapper, WorkSetupTemplate,Long> implements IWorkSetupTemplateService {

    private static final Pattern PATTERN_OF_DATE = Pattern.compile("^(\\d{4}-\\d{2}-\\d{2})(,\\d{4}-\\d{2}-\\d{2})*$");

    @Override
    public void saveOrUpdateWorkSetupTemplate(WorkSetupTemplateSaveOrUpdateVo saveOrUpdateVo, Principals.Principal principal) {
        if(saveOrUpdateVo.getWorkSetupTemplateId() == null ){
            Validate.notNull(saveOrUpdateVo.getAreaTeamId(),"Required areaTeamId");
            Validate.notNull(saveOrUpdateVo.getRemindConfig(),"Required remindConfig");
            Validate.notNull(saveOrUpdateVo.getTimeConfig(),"Required timeConfig");
        }

        if(StringUtils.isNoneEmpty(saveOrUpdateVo.getExcludeDates()) && !PATTERN_OF_DATE.matcher(saveOrUpdateVo.getExcludeDates()).matches()){
            throw new BusException("避节日格式不正确，正确格式为:2017-08-01,2017-08-01");
        }

        WorkSetupTemplate workSetupTemplate = new WorkSetupTemplate();
        BeanUtils.copyProperties( workSetupTemplate,saveOrUpdateVo );
        workSetupTemplate.setOperatorId(principal.id);
        workSetupTemplate.setOperatorName(principal.name);

        if(workSetupTemplate.getWorkSetupTemplateId() == null ){
            checkExist(workSetupTemplate.getAreaTeamId());
            workSetupTemplate.setCreateTime(TimeUtils.now());
            workSetupTemplate.setUpdateTime(TimeUtils.now());
            insertSelective(workSetupTemplate);
        }else{
            workSetupTemplate.setUpdateTime(TimeUtils.now());
            updateSelectiveById(workSetupTemplate);
        }

    }

    private void checkExist(Long areaTeamId) {
        Example<WorkSetupTemplate> example = WorkSetupTemplate.createExample();
        example.createCriteria().andEqualTo("areaTeamId" ,areaTeamId );
        if(countByExample(example) > 0 ){
            throw new BusException("这个区域已经设置");
        }
    }

    @Override
    public List<WorkSetupTemplate> findWorkSetupTemplateByAreaTeamId(DataPermission dataPermission, Long areaTeamId) {
        Example<WorkSetupTemplate> example = WorkSetupTemplate.createExample();
        example.orderBy("createTime").desc();
        Example.Criteria criteria = example.createCriteria();
        CriteriaUtils.fillConditionForAreaTeamPermission(criteria,dataPermission.getAreaTeamIds());
        if(areaTeamId != null ){
            criteria.andEqualTo("areaTeamId" , areaTeamId );
        }
        return findListByExample(example);
    }
}